<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxCard\WxCardMemberCard;
use App\Services\WxCardMemberLevelService;
use Illuminate\Http\Request;
use App\Services\WxCardService;

/**
 * 微信卡券--会员列表数据
 * @package App\Http\Controllers\Admin
 */
class WxCardMemberListController extends BaseController
{
    protected $memberCardSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->memberCardSer = new WxCardMemberCard();
    }

    /**
     * 会员列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxCardMemberList::with(['card','user','receive'])->select('*');
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('nickname', 'like', '%' . $keyword . '%');
            }
            //所属会员卡模型关联
            $cardBaseId = $request->get('card_base_id', 0);
            if ($cardBaseId) {
                $query->where('card_base_id', $cardBaseId);
            }
            //会员卡等级
            $levelId = $request->get('level_id', 0);
            if (!empty($levelId)){
                $query->where('level_id', $levelId);
            }
            $data = $query->orderBy('id', 'DESC')->paginate($pageSize)->toArray();
            if ($data['data']) {
                $cardList = $data['data'];
                foreach ($cardList as $k => $v) {
                    $v['user_card_status_str'] = $this->memberCardSer->getStatus($v['user_card_status']);
                    $v['user_info'] = $this->memberCardSer->fomatUserInfo($v['user_info']);
                    $v['card'] = $v['card'] ? $v['card'] : [];
                    $v['user'] = $v['user'] ? $v['user'] : [];
                    $cardList[$k] = $v;
                }
                $data['data'] = $cardList;
            }

            return $data;
        }
        //卡券列表
        $this->viewData['memberCardList'] = (new WxCardService())->getCardListByType(WxCardService::CARD_TYPE_MEMBER_CARD);
        $this->viewData['memberLevelGroupList'] = (new WxCardMemberLevelService())->getGroupCacheList();

        return view('admin.wxcardmemberlist.index', $this->viewData);
    }

    /**
     * 会员统计
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function count(Request $request)
    {
        if ($request->ajax()) {
            $data = [];
            $cardBaseId = $request->card_base_id ?? 0;
            $startTime = $request->start_time ?? '';
            $endTime = $request->end_time ?? '';
            if ($cardBaseId){
                $levelList = (new WxCardMemberLevelService())->getList($cardBaseId);
                if (!empty($levelList)){
                    $todayStartTime = date('Y-m-d').' 00:00:00';
                    $todayEndTime = date('Y-m-d') .' 23:59:59';
                    //日新增统计数
                    foreach ($levelList as $v){
                        $data[] = [
                            'name' => '日新增'. $v['name'] .'数',
                            'count' => $this->memberCardSer->getCount(['cardBaseId'=>$cardBaseId,'levelId'=>$v['id'],'startTime'=>$todayStartTime,'endTime'=>$todayEndTime]),
                        ];
                    }
                    //累计新增数
                    foreach ($levelList as $v){
                        $data[] = [
                            'name' => '累计新增'. $v['name'] .'数',
                            'count' => $this->memberCardSer->getCount(['cardBaseId'=>$cardBaseId,'levelId'=>$v['id']]),
                        ];
                    }
                }
                if (!empty($startTime) && !empty($endTime)){
                    //时间段内查询新增会员数
                    foreach ($levelList as $v){
                        $data[] = [
                            'name' => '新增'. $v['name'] .'数',
                            'count' => $this->memberCardSer->getCount(['cardBaseId'=>$cardBaseId,'levelId'=>$v['id'],'startTime'=>$startTime,'endTime'=>$endTime]),
                        ];
                    }
                }
            }
            return $this->showMsg('success', '获取统计数据成功', $data, 'admin.wxcardmemberlist.count');
        }
        //卡券列表
        $this->viewData['memberCardList'] = (new WxCardService())->getCardListByType(WxCardService::CARD_TYPE_MEMBER_CARD);

        return view('admin.wxcardmemberlist.count', $this->viewData);
    }
}
